package cn.supermaster.management.common.utils;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.common.utils
 * @ClassName: ConvertUtils
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2021/1/1 23:07
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.comn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
public class ConvertUtils {
    protected static final Logger logger = LoggerFactory.getLogger(ConvertUtils.class);
    private final static ObjectMapper objectMapper = new ObjectMapper();// FastJson ObjectMapper对象

    static {
        //设置ObjectMapper对象属性
        objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    }

    /**
     * @Description: [转换为int类型]
     * @Title: toInt
     * @Author: Lance Ting
     * @Date: 2020-10-09
     * @Param: value
     * @Return: java.lang.Integer
     */
    public static Integer toInt(Object value) {
        return toInt(value, null);
    }

    /**
     * @param value        被转换的值
     * @param defaultValue 转换错误时的默认值
     * @Description: [转换为int类型]
     * @Title: convertToInt
     * @author: Lance Ting
     * @Date: 2020-10-09
     * @return: Integer
     */
    public static Integer toInt(Object value, Integer defaultValue) {
        Integer result = null;
        try {
            if (value == null) {
                return defaultValue;
            }
            if (value instanceof Integer) {
                result = (Integer) value;
            }
            if (value instanceof Number) {
                result = ((Number) value).intValue();
            }
            final String valueText = toText(value, null);
            if (CheckUtils.isEmpty(valueText)) {
                result = defaultValue;
            } else {
                result = Integer.parseInt(valueText.trim());
            }

        } catch (Exception e) {
            result = defaultValue;
        }
        return result;
    }

    /**
     * @Description: [转换为字符串]
     * @Title: toText
     * @Author: Lance Ting
     * @Date: 2020-10-09
     * @Param: value 需要转换的数据对象
     * @Param: defaultValue 默认值
     * @Return: java.lang.String
     */
    public static String toText(Object value, String defaultValue) {
        if (null == value) {
            return defaultValue;
        }
        if (value instanceof String) {
            return (String) value;
        }
        return value.toString();
    }

    /**
     * @Description: [将IPv4地址转换成字节]
     * @Title: toNumericFormatV4
     * @Author: Lance Ting
     * @Date: 2020-10-09
     * @Param: ipv4 IPv4地址
     * @Return: byte[] 字节
     */
    public static byte[] toNumericFormatV4(String ipv4) {
        if (ipv4.length() == 0) {
            return null;
        }
        byte[] bytes = new byte[4];
        String[] elements = ipv4.split("\\.", -1);
        long item;
        switch (elements.length) {
            case 1:
                item = Long.parseLong(elements[0]);
                if ((item < 0L) || (item > 4294967295L)) {
                    return null;
                }
                bytes[0] = (byte) (int) (item >> 24 & 0xFF);
                bytes[1] = (byte) (int) ((item & 0xFFFFFF) >> 16 & 0xFF);
                bytes[2] = (byte) (int) ((item & 0xFFFF) >> 8 & 0xFF);
                bytes[3] = (byte) (int) (item & 0xFF);
                break;
            case 2:
                item = Integer.parseInt(elements[0]);
                if ((item < 0L) || (item > 255L)) {
                    return null;
                }
                bytes[0] = (byte) (int) (item & 0xFF);
                item = Integer.parseInt(elements[1]);
                if ((item < 0L) || (item > 16777215L)) {
                    return null;
                }
                bytes[1] = (byte) (int) (item >> 16 & 0xFF);
                bytes[2] = (byte) (int) ((item & 0xFFFF) >> 8 & 0xFF);
                bytes[3] = (byte) (int) (item & 0xFF);
                break;
            case 3:
                for (int i = 0; i < 2; ++i) {
                    item = Integer.parseInt(elements[i]);
                    if ((item < 0) || (item > 255)) {
                        return null;
                    }
                    bytes[i] = (byte) (int) (item & 0xFF);
                }
                item = Integer.parseInt(elements[2]);
                if ((item < 0L) || (item > 65535L)) {
                    return null;
                }
                bytes[2] = (byte) (int) (item >> 8 & 0xFF);
                bytes[3] = (byte) (int) (item & 0xFF);
                break;
            case 4:
                for (int i = 0; i < 4; ++i) {
                    item = Integer.parseInt(elements[i]);
                    if ((item < 0) || (item > 255)) {
                        return null;
                    }
                    bytes[i] = (byte) (int) (item & 0xFF);
                }
                break;
            default:
                return null;
        }
        return bytes;
    }

    /**
     * @Description: [对象转换JSON字符串]
     * @Title: convertT2Text
     * @Author: Lance Ting
     * @Date: 2020-09-05
     * @Param: obj
     * @Return: java.lang.String
     */
    public static String convertT2Text(Object obj) {
        return JSON.toJSONString(obj);
    }

    /**
     * @Description: [JSON字符串转换为对象]
     * @Title: convertText2T
     * @Author: Lance Ting
     * @Date: 2020-09-05
     * @Param: text
     * @Param: clazz
     * @Return: java.lang.Object
     */
    public static <T> T convertText2T(String text, Class<T> clazz) throws JsonProcessingException {
        return objectMapper.readValue(text, clazz);
    }
}